<template>
  <div>
    <el-table :data="tableData" style="width: 100%" max-height="850">
      <el-table-column fixed prop="datetime" label="日期" width="150" />
      <el-table-column prop="publisher" label="发布者" width="150" />
      <el-table-column prop="content" label="公告信息" width="900" />
      <el-table-column label="状态" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.state === 1">已读</span>
          <span v-else>未读</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="120">

        <template slot-scope="scope">
          <el-button type="text" size="small" @click.native.prevent="viewDetails(scope.row)">查看</el-button>
          <el-button type="text" size="small" @click="editRow(scope.row)">编辑</el-button>
          <el-button type="text" size="small" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="查看反馈详情" :visible.sync="dialogVisible" width="50%" @closed="dialogClosed">
      <p>日期：{{ viewDetailsData.datetime }}</p>
      <p>用户名：{{ viewDetailsData.paper_id }}</p>
      <p>反馈信息：{{ viewDetailsData.content }}</p>
      <span slot="footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
    <el-dialog title="编辑公告信息" :visible.sync="editDialogVisible" width="50%" @closed="editDialogClosed">
      <el-form :model="editFormData" label-width="80px">
        <el-form-item label="日期">
          <el-input v-model="editFormData.datetime" disabled></el-input>
        </el-form-item>
        
        <el-form-item label="公告信息">
          <el-input v-model="editFormData.content" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveEdit">保存</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import { deleteRow } from '@/api/teacher';
import { updateEdit } from '@/api/teacher';
import { getList } from '@/api/user'
export default {
  data() {
    return {
      tableData: [
        {
          paper_id: '',
          date: '2023-07-01',
          name: '张三',
          exam: '2023-07-02进行期末考试',
          state: '未读'
        }
        // 更多数据项
      ],
      editDialogVisible: false, // 控制编辑对话框的显示与隐藏
      editFormData: {
        content: "",
        datetime: "",
        id: null,
        publisher: "",
      }, // 用于编辑时绑定的数据
      dialogVisible: false,
      viewDetailsData: {}, // 用于存储查看时的数据
      paper_id: "",
    };
  },
  async created() {
    await this.fetchData();
  },
  methods: {
    viewDetails(row) {
      this.viewDetailsData = { ...row };
      this.dialogVisible = true;
    },
    markAsCompleted(index, rows) {
      rows[index].status = '已读';
    },
    dialogClosed() {
      this.viewDetailsData = {}; // 关闭对话框时清空查看数据
    },
    editRow(row) {
      this.editFormData = { ...row }; // 复制当前行数据到编辑表单数据
      this.editDialogVisible = true; // 显示编辑对话框
    },
    editDialogClosed() {
      this.editFormData = {};
    },
    async saveEdit() {
      // 尝试调用API更新数据，使用正确的数据对象和变量名
      try {
        console.log('submitting editFormData:', this.editFormData);
        const data = {
          ...this.editFormData,
          paper_id: this.editFormData.id
        }
        const res = await updateEdit(data); // 更新为使用editFormData
        console.log(res);
        if (res.status === 200) { // 修复变量名，使用res
          this.$message.success('公告信息编辑成功！');
          this.editDialogVisible = false;
          await this.fetchData();
        } else {
          this.$message.error('编辑公告信息失败，请重试。');
        }
      } catch (error) {
        console.error('编辑保存时出错:', error);
        this.$message.error('编辑保存时发生错误，请检查网络或联系管理员。');
      }
    },
    async deleteRow(row) {
      try {
        // 假定有一个deleteRow接口用于删除数据，其中id是要删除项的唯一标识符
        let data = {
          paper_id: row.id
        }
        const res = await deleteRow(data); // 
        if (res.status === 200) {
          this.$message.success('公告信息删除成功！');
          // 刷新表格数据
          await this.fetchData();
        } else {
          this.$message.error('删除公告信息失败，请重试。');
        }
      } catch (error) {
        console.error('删除时出错:', error);
        this.$message.error('删除时发生错误，请检查网络或联系管理员。');
      }
    },
    handleClick(row) {
      console.log(row.state);
    },
    async getList() {

      const res = await getList()
      console.log(res);
      console.log('--------------------------');
      this.tableData = res.data.data
    },
    async fetchData() {
      try {
        const response = await getList();
        // 假设response.data是包含status的对象数组，且status对象中有id字段
        this.tableData = response.data.data.map(item => {
          // 根据status.id判断状态
          const statusText = item.state.id === 1 || item.state.id === 2 ? '已读' : '未读';
          // 确保item对象中包含我们想要显示的状态文本
          return {
            ...item,
            displayStatus: statusText, // 添加或覆盖displayStatus属性以供模板使用
          };
        });
      } catch (error) {
        console.error('获取数据时出错:', error);
        // 错误处理逻辑
      }
    }
  }
}
</script>